/********************************************************************************
 * @Author: Zhenzhou Liu
 * @Date: 2024-12-28
 * @Description: Cg图论练习之单源最短路径（Dijkstra算法）
 * TODO：Cg测试结果错误，每个测试样例的期望输出都是0
 *******************************************************************************/
#include <iostream>
#include <vector>
#include <climits>
#include <queue>

using namespace std;

typedef pair<int,int> pii;

int dijkstra(const vector<vector<int>> & adj, int src, int dest) {
    int n = adj.size();
    vector<int> dist(n, INT_MAX);
    vector<bool> visited(n, false);
    vector<int> pred(n, -1);
    dist[src] = 0;
    priority_queue<pii, vector<pii>, greater<pii>> pq;
    pq.emplace(0, src);

    while (!pq.empty()) {
        int u = pq.top().second;
        pq.pop();
        if (visited[u])
            continue;
        visited[u] = true;

        for (int v = 0; v < n; v++) {
            if (!visited[v] && adj[u][v] != 0 && dist[u] + adj[u][v] < dist[v]) {
                dist[v] = dist[u] + adj[u][v];
                pq.emplace(dist[v], v);
                pred[v] = u;
            }
        }
    }
    return dist[dest];
}

int main(int argc, char *argv[]) {
    int n;
    cin >> n;
    vector<vector<int>> adj(n, vector<int>(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> adj[i][j];
        }
    }
    int src, dest;
    cin >> src >> dest;
    cout << dijkstra(adj, src, dest);
    return 0;
}


/*Test Case1
3
0 1 4
1 0 2
3 1 0
0 2
*/

